const WordModel = require("../../models/word/WordModel.js");

const WordDB = {
  async getWordList(params) {
    let { page, pageSize } = params; // 页码,每页条数
    if (!page && !pageSize) {
      return WordModel.find();
    }
    page = +page;
    pageSize = +pageSize;
    let skip = (page - 1) * pageSize;

    let config = {};
    if (params?.username) {
      config.username = { $regex: new RegExp(params.username, "gi") };
    }
    if (params?.updatetime && params?.updatetime?.length !== 0) {
      config.updatetime = {
        $gte: +Date.parse(params.updatetime[0]),
        $lte: +Date.parse(params.updatetime[1]),
      };
    }

    return WordModel.find(config).skip(skip).limit(pageSize);
  },
  async getCount() {
    return WordModel.find().countDocuments();
  },
  async deleteWordListItem(id) {
    return WordModel.deleteOne({ _id: id });
  },
  async addWordList(params) {
    return WordModel.create(params);
  },
  async updateWordItem(id, params) {
    return WordModel.updateOne({ _id: id }, { ...params });
  },
};

module.exports = WordDB;
